<?php
/**
 * 权益内容
 * User: Acer
 * Date: 2022/7/17
 * Time: 14:32
 */
namespace app\admin\service;

use app\admin\models\EquityModel;
use app\admin\models\ExpressModel;
use app\card\models\CardProModel;
use app\card\models\UserCardModel;
use app\move\models\CardEquityUserInfoModel;
use app\move\models\CardEquityUserModel;
use app\card\models\CardModel;
use app\move\models\CardEquityModel;
use app\move\models\IdentityCardModel;
use app\move\models\FamilyCardModel;
use app\admin\service\KdniaoService;

class EquityService{

    // 获取权益列表
    public function s_get_list($page,$paginate = 10,$like){
        $EquityModel = new EquityModel();
        // 用户名称
        if(isset($like['title'])&& !empty($like['title'])){
            $EquityModel->whereLike('title','%'.$like['title'].'%');
        }
        $list = $EquityModel->paginate($paginate,false,['page'=>$page]);
        return obj_to_arr($list);
    }
    // 创建权益
    public function s_add_equity($data){
        // 判断权益名称是否存在
        $EquityModel = new EquityModel();
        $equityInfo = $EquityModel->where('title',$data['title'])->find();
        $equityInfo = obj_to_arr($equityInfo);
        if($equityInfo) json_err(-210,["权益名称已被使用！"]);
        // 新增权益
        $installData = [
            'title'     =>  $data['title'],
            'describe'  =>  isset($data['describe']) ? $data['describe'] : '',
            'icon'      =>  isset($data['icon']) ? $data['icon'] : '',
            'state'     =>  isset($data['state']) ? $data['state'] : 0,
            'frequency' =>  isset($data['frequency']) ? $data['frequency'] : 1,
        ];
        return  $EquityModel->insertGetId($installData);
    }

    // 修改权益状态
    public function s_state_equity($data){
        $EquityModel = new EquityModel();
        $state = $EquityModel->where('eid',$data['eid'])->value('state');
        return $EquityModel->where('eid',$data['eid'])->update([
            'state' =>  (int)$state == 0 ? 1 : 0
        ]);
    }
    // 修改权益内容
    public function s_modify_equity($data){
        $EquityModel = new EquityModel();
        $equityInfo = $EquityModel->where('title',$data['title'])->where('eid','<>',$data['eid'])->find();
        $equityInfo = obj_to_arr($equityInfo);
        if($equityInfo) json_err(-210,["权益名称已被使用！"]);
        $installData = [
            'title'     =>  $data['title'],
            'describe'  =>  isset($data['describe']) ? $data['describe'] : '',
            'icon'      =>  isset($data['icon']) ? $data['icon'] : '',
            'state'     =>  isset($data['state']) ? $data['state'] : 0,
            'frequency' =>  isset($data['frequency']) ? $data['frequency'] : 1,
        ];
        $EquityModel->where('eid',$data['eid'])->update($installData);
    }

    // 权益下拉列表
    public function s_get_select_list(){
        $EquityModel = new EquityModel();
        $list = $EquityModel->where('state',1)->select();
        $list = obj_to_arr($list);
        return $list;
    }

    // 用户使用权益并填写资料列表
    public function get_equity_info_list($page,$paginate,$ctid){
	$list = (new CardEquityUserInfoModel())->where('ct_id','in',explode(',',$ctid))->paginate($paginate,false,['page'=>$page])->each(function($item, $key){
            $card_equity_user = obj_to_arr((new CardEquityUserModel())->where('ue_id',$item['ue_id'])->find());
            // 获取卡片信息
            $item['card_info'] = obj_to_arr((new CardModel())->where('cid',$card_equity_user['cid'])->find());
            // 获取权益信息
            $item['card_equity_info'] = obj_to_arr((new CardEquityModel())->where('eid',$card_equity_user['eid'])->find());
            // 卡片权益使用记录
            $item['card_equity_user'] = $card_equity_user;
            // 卡片关联信息
            $item['user_card'] = obj_to_arr((new UserCardModel())->where('cu_id',$card_equity_user['cu_id'])->find());
            // 卡片信息
            $item['card_product'] = obj_to_arr((new CardProModel())->where('pid',$item['user_card']['pid'])->find());
        });
        return obj_to_arr($list);
    }

    // 获取审核列表
    public function get_equity_info_audit_list($page,$paginate,$ctid,$name,$state,$logistic_code,$product_code){
        $CardEquityUserInfoModel = new CardEquityUserInfoModel();
        $CardEquityUserInfoModel->alias('a')->where('a.ct_id','in',explode(',',$ctid));
        if($name){
            $CardEquityUserInfoModel->whereLike('a.name',"%{$name}%");
        }
        if($state!==''){
            $CardEquityUserInfoModel->join('card_equity_user ceu', 'ceu.ue_id=a.ue_id');
            $CardEquityUserInfoModel->where('ceu.state',$state);
        }
        if($logistic_code!==''){
            $CardEquityUserInfoModel->join('card_equity_express cee', 'cee.ue_id=a.ue_id');
        }
        if($product_code!==''){
            if($state===''){
                $CardEquityUserInfoModel->join('card_equity_user ceu', 'ceu.ue_id=a.ue_id');
            }
            $CardEquityUserInfoModel->join('user_card uc','uc.cu_id=ceu.cu_id');
            $CardEquityUserInfoModel->join('card_product cp','cp.pid=uc.pid');
            $CardEquityUserInfoModel->whereLike('cp.code',"%{$product_code}%");
        }

        $list = $CardEquityUserInfoModel->paginate($paginate,false,['page'=>$page])->each(function($item, $key){
            $card_equity_user = obj_to_arr((new CardEquityUserModel())->where('ue_id',$item['ue_id'])->find());
            // 获取卡片信息
            $item['card_info'] = obj_to_arr((new CardModel())->where('cid',$card_equity_user['cid'])->find());
            // 获取权益信息
            $item['card_equity_info'] = obj_to_arr((new CardEquityModel())->where('eid',$card_equity_user['eid'])->find());
            // 卡片权益使用记录
            $item['card_equity_user'] = $card_equity_user;
            // 卡片关联信息
            $item['user_card'] = obj_to_arr((new UserCardModel())->where('cu_id',$card_equity_user['cu_id'])->find());
            // 卡片信息
            $item['card_product'] = obj_to_arr((new CardProModel())->where('pid',$item['user_card']['pid'])->find());
            // 已申请审核通过数量
            $item['audit_num'] = (new CardEquityUserModel())->where(['cu_id'=>$card_equity_user['cu_id'],'cid'=>$card_equity_user['cid'],'state'=>1])->count();
            // 主权益人身份消息
            if(isset($item['user_card']['is_other'])&&$item['user_card']['is_other']=='1'){
                $familyCardModel = obj_to_arr((new FamilyCardModel())->where(['pid'=>$item['user_card']['pid'],'puid'=>$item['user_card']['uid'],'type'=>999])->find());
                $item['identity_card'] = obj_to_arr((new IdentityCardModel())->where('did',$familyCardModel['did'])->find());
            }else{
                $item['identity_card'] = obj_to_arr((new IdentityCardModel())->where('uid',$item['user_card']['uid'])->find());
            }
            // 主权益人
            $item['main_equity_name'] = $item['identity_card']['name'];
        });
        return obj_to_arr($list);
    }

    // 导出权益服务审核列表
    public function export_equity_info_audit_list($data){
        $res = [];
        $CardEquityUserInfoModel = new CardEquityUserInfoModel();
        if(isset($data['uei_id'])&&!empty($data['uei_id'])){
            $CardEquityUserInfoModel->where('uei_id','in',explode(',',$data['uei_id']));
        }
        foreach (obj_to_arr($CardEquityUserInfoModel->select()) as $key => $item) {
            $relationType = array('本人','父母','子女','配偶');
            $state = array('审核中','审核通过','审核失败');
            $sex = array('男','男','女');
            $card_equity_user = obj_to_arr((new CardEquityUserModel())->where('ue_id',$item['ue_id'])->find());
            // 获取权益信息
            $card_equity_info = obj_to_arr((new CardEquityModel())->where('eid',$card_equity_user['eid'])->find());
            // 主权益人身份消息
            $user_card = obj_to_arr((new UserCardModel())->where('cu_id',$card_equity_user['cu_id'])->find());
            $identity_card = obj_to_arr((new IdentityCardModel())->where('uid',$user_card['uid'])->find());
            $upload_data = obj_to_arr(json_decode($item['upload_data']));

            $obj=[];
            if(isset($upload_data['relationType'])){
                array_push($obj,$relationType[$upload_data['relationType']]);
            }else{
                array_push($obj,'');
            }
            array_push($obj,$item['name']??'');
            array_push($obj,$identity_card['name']??'');
            array_push($obj,$card_equity_info['title']??'');
            array_push($obj,$state[$card_equity_user['state']]??'');
            array_push($obj,$item['id_card']??'');
            array_push($obj,$upload_data['age']??'');
            array_push($obj,isset($upload_data['sex']) ? $sex[$upload_data['sex']] : '');
            array_push($obj,$upload_data['chooseValue']??'');
            array_push($obj,$upload_data['Departments']??'');
            array_push($obj,$upload_data['site']??'');
            array_push($obj,$upload_data['casebearer']??'');
            array_push($obj,$item['create_time']??'');
            array_push($obj,$upload_data['city']??'');
            array_push($obj,$upload_data['emergencyContact']??'');
            array_push($obj,$item['mobile']??'');
            array_push($res, $obj);
        }
        return obj_to_arr($res);
    }

    // 审核权益服务
    public function audit_equity_info($data){
        if(isset($data['logistic_code'])&&!empty($data['logistic_code'])){
            $CardEquityUserModel = new CardEquityUserModel();
            $cardEquityUser = obj_to_arr($CardEquityUserModel->where('ue_id',$data['ue_id']));
            (new ExpressModel())->insert([
                'uid'=>$cardEquityUser['uid'],
                'ue_id'=>$cardEquityUser['ue_id'],
            ]);
        }
        return (new CardEquityUserModel())
            ->where(['ue_id' => $data['ue_id']])
            ->update([
                'state' => $data['state'],
                'audit_opinion'=>$data['audit_opinion']
            ]);
    }

    // 添加物流信息
    public function add_express_info($data){
        if (isset($data['logistic_code']) && !empty($data['logistic_code'])) {
            $ExpressModel = new ExpressModel();
            $ExpressModel->where('ue_id', $data['ue_id'])->update([
                'logistic_code' => $data['logistic_code'],
                'shipper_name' => $data['shipper_name'],
                'recipient' => $data['recipient'],
                'sender_mobile' => $data['mobile'],
                'address' => $data['address']
            ]);
        }
    }

}
